#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        // for (int i = 0; i < arr.size(); i++)
        // {
        //     if (i < arr.size() - 1 && arr[i] == 0)
        //     {
        //         int end = arr.size() - 2;
        //         while (end > i)
        //         {
        //             arr[end + 1] = arr[end];
        //             end--;
        //         }
        //         arr[i + 1] = 0;
        //         i++;
        //     }
        // }

        int count = 0;
        int index = -1;
        int end = arr.size() - 1;
        while (count < arr.size())
        {
            index++;
            count++;
            if (arr[index] == 0)
                count++;
        }

        if (count == arr.size() + 1)
        {
            arr[end] = 0;
            end--;
            index--;
        }
        while (index >= 0)
        {
            arr[end--] = arr[index];
            if (arr[index] == 0)
                arr[end--] = arr[index];
            index--;
        }
    }
};